'*******************************************************************************
' *  
' *  Product: GSALib
' *  Description: A C# API for accessing the Google Search Appliance.
' *
' *  (c) Copyright 2008 Michael Cizmar + Associates Ltd.  (MC+A)
' *  
'*******************************************************************************


Imports System
Imports System.Collections.Generic
Imports System.Collections
Imports System.Text
Imports System.Globalization

Namespace GSALib.Utils
    ''' <summary>
    ''' Class provides Util Methods to get/set and appending new parameters into GSA Query
    ''' <para>Author Albert Ghukasyan</para>    
    ''' </summary>
    Friend NotInheritable Class Util
        Private Sub New()
        End Sub
#Region "Methods"

        ''' <summary>
        ''' Returns obj.ToString() if obj not null or Default if is null
        ''' </summary>
        ''' <param name="obj">Object From wich need to get String</param>
        ''' <param name="Default">Returns when Obj is null</param>        
        ''' <returns></returns>
        Public Shared Function getString(ByVal obj As Object, ByVal [Default] As String, ByVal nullstrict As Boolean) As String
            Dim retval As String = [Default]
            If obj IsNot Nothing AndAlso (Not nullstrict OrElse Not "".Equals(obj.ToString())) Then
                retval = obj.ToString()
            End If
            Return retval
        End Function

        ''' <summary>
        ''' Allows String Separation
        ''' </summary>
        ''' <param name="tokens"></param>
        ''' <param name="prefix"></param>
        ''' <param name="delim"></param>
        ''' <returns></returns>
        Public Shared Function SeparatedString(ByVal tokens As ArrayList, ByVal prefix As String, ByVal delim As String) As String
            Dim sbuf As New StringBuilder()

            If tokens IsNot Nothing Then
                Dim i As Integer = 0, iSize As Integer = tokens.Count
                While i < iSize
                    sbuf.Append(IIf(i <= 0 OrElse i >= iSize, "", delim))
                    sbuf.Append(IIf(prefix Is Nothing, "", prefix)).Append(tokens(i).ToString())
                    i += 1
                End While
            End If
            Return sbuf.ToString()
        End Function

        ''' <summary>
        ''' Allows String Separation
        ''' </summary>
        ''' <param name="tokens"></param>
        ''' <param name="prefix"></param>
        ''' <param name="delim"></param>
        ''' <returns></returns>
        Public Shared Function SeparatedString(ByVal tokens As String(), ByVal prefix As String, ByVal delim As String) As String
            Dim sbuf As New StringBuilder()

            If tokens IsNot Nothing Then
                Dim i As Integer = 0, iSize As Integer = tokens.Length
                While i < iSize
                    sbuf.Append(IIf(i <= 0 OrElse i >= iSize, "", delim))
                    sbuf.Append(IIf(prefix Is Nothing, "", prefix)).Append(tokens(i))
                    i += 1
                End While
            End If
            Return sbuf.ToString()
        End Function

        ''' <summary>
        ''' Appends New Parameter to Query
        ''' </summary>
        ''' <param name="sbuf"></param>
        ''' <param name="param"></param>
        ''' <param name="value"></param>
        Public Shared Sub appendQueryParam(ByVal sbuf As StringBuilder, ByVal param As String, ByVal value As String)
            If sbuf.Length > 0 AndAlso "&" <> sbuf.ToString().Substring(sbuf.Length - 1, 1) Then
                sbuf.Append("&"c)
            End If
            sbuf.Append(param).Append("="c).Append(encode(value))
        End Sub

        ''' <summary>
        ''' Appends New Parameter to Query
        ''' </summary>
        ''' <param name="sbuf"></param>
        ''' <param name="param"></param>
        ''' <param name="values"></param>
        Public Shared Sub appendQueryParam(ByVal sbuf As StringBuilder, ByVal param As String, ByVal values As String())
            If param IsNot Nothing AndAlso values IsNot Nothing AndAlso values.Length > 0 Then
                Dim i As Integer = 0, iSize As Integer = values.Length
                While i < iSize
                    appendQueryParam(sbuf, param, values(i))
                    i += 1
                End While
            End If
        End Sub

        ''' <summary>
        ''' Appends New Maped Parameter to Query
        ''' </summary>
        ''' <param name="sbuf"></param>
        ''' <param name="param"></param>
        ''' <param name="fieldsMap"></param>
        ''' <param name="delimiter"></param>
        Public Shared Sub appendMappedQueryParams(ByVal sbuf As StringBuilder, ByVal param As String, ByVal fieldsMap As Hashtable, ByVal delimiter As String)
            If sbuf.Length > 0 AndAlso "&" <> sbuf.ToString().Substring(sbuf.Length - 1, 1) Then
                sbuf.Append("&")
            End If
            sbuf.Append(param).Append("=")

            Dim firstTime As Boolean = True
            For Each _key As Object In fieldsMap.Keys
                Dim key As String = _key.ToString()
                Dim value As String = fieldsMap(_key).ToString()
                If Not firstTime Then
                    sbuf.Append(encode(delimiter))
                End If

                Dim encvalue As String = encode(key + (IIf(value Is Nothing, "", ":" + value)))
                sbuf.Append(encvalue)
                firstTime = False
            Next
        End Sub

        ''' <summary>
        ''' Encodes string to URL string
        ''' </summary>
        ''' <param name="value">Value to encode</param>
        ''' <returns></returns>
        Public Shared Function encode(ByVal value As String) As String
            Dim retval As String = ""
            Try
                If value IsNot Nothing Then
                    retval = System.Web.HttpUtility.UrlEncode(value)
                End If
            Catch ex As Exception
                Throw New GSALib.Exceptions.GSAInvalidURLException("Invalid Url specified in parameter 'value' ", ex.StackTrace)
            End Try
            Return retval
        End Function

#End Region
    End Class
End Namespace
